package com.seezoon.interfaces;

import com.seezoon.application.user.dto.RefreshTokenCmd;
import com.seezoon.application.user.dto.ResetPasswordCmd;
import com.seezoon.application.user.dto.SendRegisterCodeCmd;
import com.seezoon.application.user.dto.SendResetPasswordCodeCmd;
import com.seezoon.application.user.dto.UserPwdLoginCmd;
import com.seezoon.application.user.dto.UserRegisterCmd;
import com.seezoon.application.user.dto.clientobject.LoginCO;
import com.seezoon.application.user.executor.RefreshTokenCmdExe;
import com.seezoon.application.user.executor.ResetPasswordCmdExe;
import com.seezoon.application.user.executor.SendRegisterCodeCmdExe;
import com.seezoon.application.user.executor.SendResetPasswordCodeCmdExe;
import com.seezoon.application.user.executor.UserPwdLoginCmdExe;
import com.seezoon.application.user.executor.UserRegisterCmdExe;
import com.seezoon.infrastructure.dto.Response;
import com.seezoon.infrastructure.exception.BizException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "用户登录", description = "用户登录处理")
@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    private final UserPwdLoginCmdExe userPwdLoginCmdExe;
    private final SendRegisterCodeCmdExe sendRegisterCodeCmdExe;
    private final SendResetPasswordCodeCmdExe sendResetPasswordCodeCmdExe;
    private final ResetPasswordCmdExe resetPasswordCmdExe;
    private final UserRegisterCmdExe userRegisterCmdExe;
    private final RefreshTokenCmdExe refreshTokenCmdExe;

    @Operation(summary = "发送注册验证码")
    @PostMapping("/send_register_code")
    public Response sendRegisterCode(@RequestBody SendRegisterCodeCmd cmd) {
        return sendRegisterCodeCmdExe.execute(cmd);
    }

    @Operation(summary = "发送重置密码验证码")
    @PostMapping("/send_reset_password_code")
    public Response sendResetPasswordCode(@RequestBody SendResetPasswordCodeCmd cmd) {
        return sendResetPasswordCodeCmdExe.execute(cmd);
    }

    @Operation(summary = "重置密码")
    @PostMapping("/reset_password")
    public Response resetPassword(@RequestBody ResetPasswordCmd cmd) {
        return resetPasswordCmdExe.execute(cmd);
    }

    @Operation(summary = "账号密码登录")
    @PostMapping("/user_pwd")
    public Response<LoginCO> userPwdLogin(@RequestBody UserPwdLoginCmd cmd) {
        return userPwdLoginCmdExe.execute(cmd);
    }

    @Operation(summary = "用户注册")
    @PostMapping("/user_register")
    public Response userRegister(@RequestBody UserRegisterCmd cmd) {
        return userRegisterCmdExe.execute(cmd);
    }

    @Operation(summary = "刷新令牌")
    @PostMapping("/refresh_token")
    public Response<LoginCO> refreshToken(@RequestBody RefreshTokenCmd cmd, HttpServletResponse response) {
        try {
            return refreshTokenCmdExe.execute(cmd);
        } catch (BizException e) {
            // 业务错误返回401
            log.error("refresh token error {},{}", e.getcode(), e.getMessage());
            response.setStatus(401);
            return null;
        }
        // 其他错误正常报错，避免DB 抖动造成用户闪退
    }
}
